package cn.sxt.yxl.controller.head;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sxt.yxl.common.controller.BaseController;
import cn.sxt.yxl.common.util.ConstaFinalUtil;
import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.question.pojo.AColomns;
import cn.sxt.yxl.question.pojo.AColumnsEnum;
import cn.sxt.yxl.question.pojo.AItems;
import cn.sxt.yxl.question.pojo.AItemsEnum;
import cn.sxt.yxl.question.service.IQuestionService;
import cn.sxt.yxl.user.service.IUserService;

@Controller
public class IndexHeadController extends BaseController{
	@Resource
	private IQuestionService questionService;
	
	
	@RequestMapping("/main")
	public String main(HttpServletRequest request){
		ConstaFinalUtil.LOGGER.info("---- main -----");
	
		
		
		return "/head/main";
	}
	
	
	/**
	 * 查询所有的栏目
	 * @return
	 */
	@RequestMapping("/header")
	public String header(HttpServletRequest request)
	{
		/* 接收一个参数 */
		String id = request.getParameter("id");
		ConstaFinalUtil.LOGGER.info("---- header ---id:{}-",id);
		/* 将所有的栏目查询出来 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 只查询状态为启用的 */
		condMap.put("status", AColumnsEnum.STATUS_ENABLE.getStatus());		
		List<AColomns> columnsList = this.questionService.findCondListColomnsService(null, condMap);
		/* 将查询出的结果放到Request中 */
		request.setAttribute("columnsList", columnsList);
		
		/*return "../../common/include/header" ;*/
		return "/head/header" ;
	}
	/*栏目下面的测试项列表*/
	@RequestMapping("/{url}")
	public String itemsList(HttpServletRequest request, @PathVariable String url){
		ConstaFinalUtil.LOGGER.info("---- itemsList -----url{}",url);
		Map<String, Object> condMap = new HashMap<String, Object>();
		//查询url对应的栏目
		condMap.clear();
		condMap.put("url", url);
		
		
		AColomns columns=this.questionService.findOneColomnsService(condMap);
		request.setAttribute("columns", columns);
		//分页信息
		PageInfoUtil pageInfoUtil=this.proceedPageInfo(request);
		//查询url对应栏目下的测试信息
		condMap.clear();
		condMap.put("columnsId", columns.getId());
		condMap.put("status", AItemsEnum.STATUS_ENABLE.getStatus());
		List<AItems>itemsList = this.questionService.findItemsCondListService(pageInfoUtil, condMap);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		request.setAttribute("itemsList", itemsList);
		return "/head/itemsList";
	}
	/*测试详情页*/
	@RequestMapping("/items/{iid}")
	public String itemsInfo(HttpServletRequest request, @PathVariable int iid){
		ConstaFinalUtil.LOGGER.info("---- itemsInfo -----");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("id", iid);
		AItems items=this.questionService.findOneItemsService(condMap);
		request.setAttribute("items", items);
		
		//查询上一条
		PageInfoUtil pageInfoUtil=new PageInfoUtil();
		condMap.clear();
		condMap.put("pubTimeDa", items.getPubTime());
		
		condMap.put("orderby", "pubTimeASC");
		List<AItems> itemPreList=this.questionService.findItemsCondListService(pageInfoUtil, condMap);
		request.setAttribute("itemPreList", itemPreList);
		//查询下一条
		condMap.clear();
		condMap.put("pubTimeXiao", items.getPubTime());
		List<AItems> itemNextList=this.questionService.findItemsCondListService(pageInfoUtil, condMap);
		request.setAttribute("itemNextList", itemNextList);
		return "/head/itemsInfo";
	}
}
